package com.wzq.web.filedownupload;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/download.do")
public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileName = req.getParameter("filename");
        if(fileName==null){
            return;
        }
        String path = "img/";
        //1、创建对象
        SmartUpload su = new SmartUpload();
        //2、初始化
        su.initialize(getServletConfig(),req,resp);
        //3、设置请求头 content-type 为通用二进制流
        resp.setContentType("application/octet-stream");
        //3、下载的时候需要设置为附件，就会弹出对话框
        su.setContentDisposition("attachment;");
        //4、开始下载文件到客户端
        try {
            su.downloadFile(path+fileName);
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }
}
